package cn.wgx.module.ent.web;

import cn.wgx.common.base.IBaseController;
import cn.wgx.common.base.IBaseNoneController;
import cn.wgx.common.entity.ent.EntInvite;
import cn.wgx.common.entity.ent.Enterprise;
import cn.wgx.common.security.client.config.entity.LoginAppUser;
import cn.wgx.common.security.util.SysUserUtil;
import cn.wgx.common.util.ResponseDataUtil;
import cn.wgx.module.ent.service.EntInviteService;
import cn.wgx.module.ent.service.EnterpriseService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@Api(tags = "5.2.2 企业用户邀请管理")
@RestController
@RequestMapping("/api/entInvite")
public class EntInviteCtrl extends IBaseNoneController<EntInviteService, EntInvite> {

    @Autowired
    EnterpriseService enterpriseService;

    @ApiOperation(value = "接受邀请", notes = "必填：invite_id")
    @PatchMapping("accept")
    public Map accept(@RequestParam Integer invite_id) {

        LoginAppUser loginAppUser = SysUserUtil.getLoginAppUser(null);
        EntInvite byId = service.getById(invite_id);
        //空 、状态（只有0时才可以操作）
        //判断当前登录者是否是被邀请的用户
        if (byId != null && !loginAppUser.getUser_id().equals(byId.getUser_id())) {
            return ResponseDataUtil.returnErrorMap("非邀请用户不可以更改邀请状态");
        }
        if (byId != null && byId.getStatus() != 0) {
            return ResponseDataUtil.returnErrorMap("此邀请状态下不可更改");
        }
        return ResponseDataUtil.returnData(service.accept(invite_id));
    }

    @ApiOperation(value = "拒绝邀请", notes = "必填：invite_id")
    @PatchMapping("reject")
    public Map reject(@RequestParam Integer invite_id) {

        LoginAppUser loginAppUser = SysUserUtil.getLoginAppUser(null);
        EntInvite byId = service.getById(invite_id);
        //空 、状态（只有0时才可以操作）
        //判断当前登录者是否是被邀请的用户
        if (byId != null && !loginAppUser.getUser_id().equals(byId.getUser_id())) {
            return ResponseDataUtil.returnErrorMap("非邀请用户不可以更改邀请状态");
        }
        if (byId != null && byId.getStatus() != 0) {
            return ResponseDataUtil.returnErrorMap("此邀请状态下不可更改");
        }
        return ResponseDataUtil.returnData(service.reject(invite_id));
    }

    @ApiOperation(value = "获取邀请我的列表", notes = "status：0待处理，1已接受,2已拒绝")
    @GetMapping("invite")
    public Map invite(@ModelAttribute EntInvite entInvite,
                      @RequestParam(required = false, defaultValue = "1") Integer page,
                      @RequestParam(required = false, defaultValue = "20") Integer limit) {
        Page page1 = new Page(page, limit);
        return ResponseDataUtil.returnData(service.invite(page1, new QueryWrapper<>(entInvite)));
    }

    @ApiOperation(value = "获取我邀请的列表", notes = "status：0待处理，1已接受,2已拒绝")
    @GetMapping("iInvite")
    public Map iInvite(@ModelAttribute EntInvite entInvite,
                       @RequestParam(required = false, defaultValue = "1") Integer page,
                       @RequestParam(required = false, defaultValue = "20") Integer limit) {
        Page page1 = new Page(page, limit);
        return ResponseDataUtil.returnData(service.iInvite(page1, new QueryWrapper<>(entInvite)));
    }

    @ApiOperation(value = "新增实体数据", notes = "必填：ent_id,user_id")
    @PostMapping("add")
    public Map add(@RequestBody EntInvite entInvite) {
        LoginAppUser loginAppUser = SysUserUtil.getLoginAppUser(null);
        Enterprise byId = enterpriseService.getById(entInvite.getEnt_id());
        if (!loginAppUser.getUser_id().equals(byId.getCreateUserId())) {
            return ResponseDataUtil.returnErrorMap("非企业管理员不可以操作");
        }
        QueryWrapper<EntInvite> queryWrapper = new QueryWrapper<>();
        if (entInvite.getEnt_id() != null && entInvite.getUser_id() != null) {
            QueryWrapper<EntInvite> eq = queryWrapper.eq("ent_id", entInvite.getEnt_id()).eq("user_id", entInvite.getUser_id()).eq("status", 0);
            if (service.getOne(eq) != null) {
                return ResponseDataUtil.returnErrorMap("请勿重复操作，等待用户接受邀请");
            }
        }
        return ResponseDataUtil.returnData(service.save(entInvite));
    }

    @ApiOperation(value = "新增实体数据-批量", notes = "必填：ent_id,user_id")
    @PostMapping("addBatch")
    public Map addBatch(@RequestBody List<EntInvite> t) {
        if (t == null) {
            return ResponseDataUtil.returnErrorMap("至少选择一个");
        }
        LoginAppUser loginAppUser = SysUserUtil.getLoginAppUser(null);
        for (EntInvite entInvite : t) {
            Enterprise byId = enterpriseService.getById(entInvite.getEnt_id());
            if (byId != null && !loginAppUser.getUser_id().equals(byId.getCreateUserId())) {
                return ResponseDataUtil.returnErrorMap("非企业管理员不可以操作");
            }
            QueryWrapper<EntInvite> queryWrapper = new QueryWrapper<>();
            if (entInvite.getEnt_id() != null && entInvite.getUser_id() != null) {
                QueryWrapper<EntInvite> eq = queryWrapper.eq("ent_id", entInvite.getEnt_id()).eq("user_id", entInvite.getUser_id()).eq("status", 0);
                if (service.getOne(eq) != null) {
                    return ResponseDataUtil.returnErrorMap("有重复邀请操作，等待用户接受邀请");
                }
            }
        }
        service.saveBatch(t);
        return ResponseDataUtil.returnData(t);
    }

}
